home *** CD-ROM | disk | FTP | other *** search
- {*********************************************************}
- {* AAHpWin *}
- {* Copyright (c) Julian M Bucknall 2000 *}
- {* All rights reserved. *}
- {*********************************************************}
- {* Algorithms Alfresco: Heap manager using Windows heaps *}
- {*********************************************************}
-
- {Note: this unit is released as freeware. In other words, you are free
- to use this unit in your own applications, however I retain all
- copyright to the code. JMB}
-
- unit AAHpWin;
-
- {WARNING: this unit *must* appear first in your project's uses list.}
-
- interface
-
- implementation
-
- uses
- Windows; // it's OK to use the Windows unit: it allocates no memory
-
- var
- OrigHeap : TMemoryManager;
- OurHeap : TMemoryManager;
- HeapHandle : THandle;
-
- function OurGetMem(Size : integer) : pointer;
- begin
- Result := HeapAlloc(HeapHandle, 0, Size);
- end;
-
- function OurFreeMem(P : pointer) : integer;
- begin
- if HeapFree(HeapHandle, 0, P) then
- Result := 0
- else
- Result := 1;
- end;
-
- function OurReallocMem(P : pointer; Size : integer) : pointer;
- begin
- Result := HeapRealloc(HeapHandle, 0, P, Size);
- end;
-
- procedure InitializeUnit;
- begin
- {get the original manager}
- GetMemoryManager(OrigHeap);
-
- {set up our heap manager}
- OurHeap.GetMem := OurGetMem;
- OurHeap.FreeMem := OurFreeMem;
- OurHeap.ReallocMem := OurReallocMem;
-
- {create a Windows heap}
- HeapHandle := HeapCreate(0, 1024*1024, 0);
-
- {replace heap manager with ours}
- if (longint(HeapHandle) <> 0) then
- SetMemoryManager(OurHeap);
- end;
-
- procedure FinalizeUnit;
- begin
- {restore the original manager}
- SetMemoryManager(OrigHeap);
-
- {dispose of the Win32 heap}
- if (longint(HeapHandle) <> 0) then
- HeapDestroy(HeapHandle);
- end;
-
- initialization
- InitializeUnit;
-
- finalization
- FinalizeUnit;
-
- end.
-